package com.bl.utils;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author fanhaotian
 */
@Component
public class PhoneCodeUtil {
     public String send(String mobile, String minute) {
          StringBuffer code = new StringBuffer();
          Random random = new Random();
          for (int i = 0; i < 6; i++) {
               code.append(random.nextInt(10));
          }
          String host = "https://gyytz.market.alicloudapi.com";
          String path = "/sms/smsSend";
          String method = "POST";
          String appcode = "4a5d33c7e84b495b8f45552322858b9f";
          Map<String, String> headers = new HashMap<String, String>();
          //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
          headers.put("Authorization", "APPCODE " + appcode);
          Map<String, String> querys = new HashMap<String, String>();
          querys.put("mobile", mobile);
          querys.put("param", "**code**:" + code + ",**minute**:" + minute);
          querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
          querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
          Map<String, String> bodys = new HashMap<String, String>();
          try {
               HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
               //System.out.println(response.toString());
               //获取response的body
               System.out.println(EntityUtils.toString(response.getEntity()));
               return code.toString();
          } catch (Exception e) {
               e.printStackTrace();
          }
          return null;
     }
}
